<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>随机数求和</title>
</head>

<body>
  <button onclick="sum()">N 个随机 4 位整数/和</button>
  <p id="result"></p>

  <script>
    function sum() {
      let n = parseInt(prompt("输入 n："));
      let total = 0;
      let randomNumbers = [];

      for (let i = 0; i < n; i++) {
        let randomNumber = Math.floor(Math.random() * 9000) + 1000;
        total += randomNumber;
        randomNumbers.push(randomNumber);
      }

      let output = "随机数: " + randomNumbers.join(", ") + "<br>总和: " + total;
      document.getElementById("result").innerHTML = output;
    }
  </script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>随机数求和</title>
</head>

<body>
  <button onclick="sum()">N 个随机 4 位整数/和</button>
  <p id="result"></p>

  <script>
    function sum() {
      let n = parseInt(prompt("输入 n："));
      let total = 0;
      let randomNumbers = [];

      for (let i = 0; i < n; i++) {
        let randomNumber = Math.floor(Math.random() * 9000) + 1000;
        total += randomNumber;
        randomNumbers.push(randomNumber);
      }

      let output =n+ "个随机数: " + randomNumbers.join(", ") + "<br>总和: " + total;
      document.getElementById("result").innerHTML = output;
    }
  </script>
</body>

</html>
